# 进程间通信
from multiprocessing import Process, Queue
import os, time, random

def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)
        time.sleep(random.random())

def read(q):
    print('Process to read %s' % os.getpid())
    # 死循环，可以一直获取
    while True:
        print(q.get(True))

if __name__ == "__main__":
    q = Queue()
    # 写子进程
    write_p = Process(
        target=write,
        args=(q, )
    )
    # 读子进程
    read_p = Process(
        target=read,
        args=(q,)
    )
    # 启动子进程
    write_p.start()
    read_p.start()
    # 等待进程结束
    write_p.join()
    # read_p为死循环， 需要手动关闭进程
    read_p.terminate()
    